package manager;

import java.util.Arrays;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

public class ConnectionManager {
	static int size = 10;
	static Connection connectionArray[];

	static public String myToString() {
	    return "ConnectionManager [size=" + size + ", connectionArray="
	            + Arrays.toString(connectionArray) + "]";
    }
	
	static public void initialize(int size) {
		ConnectionManager.size = size;
		
		ConnectionManager.connectionArray = new Connection[size];
		
		for (int i = 0; i < ConnectionManager.connectionArray.length; i++) {
			ConnectionManager.connectionArray[i] = new Connection();
		}
	
	}

	static public Connection getConnection() {
	
		Connection conn = null;
				
		int connNumber = 0;
		for (Connection c : connectionArray) {
			if (!c.isInUse()) {
				conn = c;
				break;
			}	
			connNumber++;
		}
		
		
		if (conn == null) {
			System.out.println("Out of connections");
			return null;
		}
		else {
			conn.setInUse(true);
			System.out.println("Fetched connection with number " + connNumber);
			return conn;
		}
	}
	
}
